package com.edu.eduservice.controller;



import com.edu.common.utils.R;
import com.edu.eduservice.entity.EduChapter;
import com.edu.eduservice.vo.chapter.ChapterVo;
import com.edu.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-05-30
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
@Api(description = "课程章节管理")
public class EduChapterController {

  @Autowired
   private EduChapterService chapterService;

    /**
     * 删除章节
     * @param chapterId
     * @return
     */
  @ApiOperation("删除章节")
  @DeleteMapping("/{chapterId}")
  @Transactional
  public R delChapter(@ApiParam(value = "chapterId",name = "删除章节",required = true)
                    @PathVariable("chapterId") String chapterId){
     Boolean b = chapterService.delChapter(chapterId);
        if (b){
            return R.ok();
        }else {
            return R.error();
        }

  }


    /**
     * 修改章节
     * @param chapter
     * @return
     */
  @ApiOperation("修改章节")
  @PostMapping("/updateChapter")
  @Transactional
  public R updateChapter(@ApiParam(value = "chapter",name = "修改章节",required = true)
                         @RequestBody EduChapter chapter){
        chapterService.updateById(chapter);

        return R.ok();
  }



    /**
     * 查询章节
     * @param chapterId
     * @return
     */
  @ApiOperation("查询章节")
  @GetMapping("/getChapter/{chapterId}")
  public R getChapter(@ApiParam(value = "chapterId",name = "章节id",required = true)
                        @PathVariable("chapterId") String chapterId){
      EduChapter chapter = chapterService.getById(chapterId);

      return R.ok().data("chapter",chapter);
  }



    /**
     * 添加章节
     * @param chapter
     * @return
     */
  @ApiOperation(value = "添加章节")
  @PostMapping("/addChapter")
  @Transactional
  public R addChapter(@ApiParam(value = "chapter",name = "章节",required = true)
                    @RequestBody EduChapter chapter){
      chapterService.save(chapter);
      return R.ok();
  }



 /**
  * 查询课程中的章节和小节
  * @return
  */
 @ApiOperation(value = "嵌套章节数据列表")
 @GetMapping("/getChapterVideo/{courseId}")
      public R getChapterVideo(@PathVariable String courseId){
        List<ChapterVo> list= chapterService.getChapterVideoByCourseId(courseId);
         return  R.ok().data("ChapterVideo",list);

       }




   }

